/**
*/
(function ($) {
	$(window).load(function () {
	  ksg.ui.pageLoaded = true;
	});
    var methods = {
        init: function (options) {
            var $this = this;
			if(options.useLocalScripts == null)
				options.useLocalScripts = true;
            if(!ksg.ui._setupScripts) {
                ksg.ui._setupScripts = true;
				ksg.services.baseUrl = options.serviceUrl;
                methods._setupScripts.apply(this, [options, function() {
					ksg.App.configMgr.init(options);
                    methods._setupWidgets.apply($this, [options]);
					
					//Get the bizproc coordinator going
					ksg.App.bizProcMgr.findAndInitDomBizProcs(true);
                }]);
            }
			else
				methods._setupWidgets.apply($this, [options]);
			return this;
        },

		_remoteLightBoxBaseUrl: "http://scscode.syrinx.ph/fancybox/",
		
        _setupScripts: function(options, success) {
			var self = this, lightBoxBaseUrl = self._remoteLightBoxBaseURl;
            if(options.includeGoogleFonts) {
                var f = options.includeGoogleFonts;
                for(var i = 0; i < f.length; i++)
                    ksg.ui.loadCssUrl("http://fonts.googleapis.com/css?family=" + f[i]);
            }
                        
            //Right now fancybox is the default lightbox, but could be changed to anything in the future.
            if(options.includeLightBox) {
				if(options.useLocalScripts == true)
					lightBoxBaseUrl = "/fancybox/";

				ksg.ui.loadCssUrl(lightBoxBaseUrl+"jquery.fancybox-1.3.4.css");
				ksg.ui.loadScriptUrl(lightBoxBaseUrl+"jquery.fancybox-1.3.4.pack.js", success);
            }
            else
                success();
        },

        _setupWidgets: function( options) {
            return this.each(function () {

                var $this = $(this),
                data = $this.data('autoOp');

                // If the plugin hasn't been initialized yet
                if (!data) {
                    /* Do more setup stuff here
                    */
                    $this.data('autoOp', true);
                    var op = $this.data("options");
                    var opt = typeof(op);
                    if(opt == "string")
                        opt = $.parseJSON(opt);
                    if(op == null) {
                        var t = $this.find("textArea")                        
                        if(t && t.val() != null) {
                            op = $.parseJSON(t.val());
                            t.remove();
                        }
                    }


                    var classList =$this.attr('class').split(/\s+/);
                    $.each( classList, function(){
                        var cName = this.toString();
                        if(ksg.ui.widgetMap[cName]) {
                            ksg.ui.widgetMap[cName]($this, op);
                            $this.bind("tmplApplied", function() {
                                //Fancybox should never be referenced directly by code.  Another lightbox could be used, and this code file
                                //should be all that needs to change changed.
                                if(options.includeLightBox == true)
                                    $this.find(".ksg-image").fancybox();
                            });
                            return false;
                        }
                    });
                }
            });
        },

        destroy: function () {

            return this.each(function () {
                var $this = $(this),
                data = $this.data('autoOp');
                $this.removeData('autoOp');
            })

        }
    };

    $.fn.ksgSetupUI = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.ksgSetupUI');
        }

    };


})(jQuery);
